<template>
  <div class="school-info">
    <titleHeader name="学科信息" />
    <div class="subject-info">
      <div
        v-for="(item, index) in subjectInfoList"
        :key="index"
        class="subject-item"
      >
        <div class="title">{{ item.title }}</div>
        <div class="num" v-show="index != 0">{{ item.num }}<span>个</span></div>
        <div class="stars" v-show="index == 0">
          <!-- 循环渲染 activeStar -->
          <img
            v-for="i in item.num"
            :key="'active-star-' + i"
            class="star"
            :src="activeStar"
            alt=""
          />
          <!-- 循环渲染剩余的普通 star -->
          <img
            v-for="i in item.leftNum"
            :key="'star-' + i"
            class="star"
            :src="star"
            alt=""
          />
        </div>
      </div>
    </div>
    <titleHeader name="排名信息" />
    <div class="rank-info">
      <div v-for="(item, index) in rankList" :key="index" class="rank-item">
        <div class="rank">{{ item.rank == '0' ? '-' : item.rank }}</div>
        <div class="title">{{ item.title }}</div>
      </div>
    </div>
    <titleHeader name="校园配置" />
    <div class="campus-info">
      <div class="bar">
        <span class="title">校区</span>
        <div class="select">
          <MySelect
            v-model="currentCampus"
            :options="campusOptions"
            labelKey="campusName"
            valueKey="id"
            placeholder="请选择校区"
          />
        </div>
      </div>
      <div class="facility">
        <div
          v-for="(item, index) in facilityList"
          :key="index"
          class="container"
        >
          <div class="facility-item" v-if="item.show">
            <div class="icon-container">
              <img class="icon" :src="item.icon" alt="" />
            </div>
            <div class="name">{{ item.name }}</div>
          </div>
        </div>
      </div>
      <div class="dormitory">
        <div class="title">
          <div>
            <img
              class="logo"
              src="https://img3.job1001.com/ypMp/elan/images/career/collegeVolunteer/sushe.png"
              alt=""
            />
          </div>
          宿舍
        </div>
        <div class="desc">
          <van-text-ellipsis
            rows="3"
            :content="dormitoryDesc"
            expand-text="展开"
            collapse-text="收起"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';
import { useRoute } from 'vue-router'; // 导入路由
import { getCollegeCampusList } from '@/api/collegeSearch';
import star from '@/assets/images/star.png';
import activeStar from '@/assets/images/activeStar.png'; // 导入图片
import titleHeader from './TitleHeader.vue';
import MySelect from '@/components/MySelect.vue';

const props = defineProps({
  schoolInfoParams: {
    type: Object,
    default: () => {},
  },
});
const currentCampus = ref('');
const campusOptions = ref([]);
const facilityList = ref([
  {
    icon: 'https://img3.job1001.com/ypMp/elan/images/career/collegeVolunteer/bathroom.png',
    name: '有独立卫浴',
    show: false,
  },
  {
    icon: 'https://img3.job1001.com/ypMp/elan/images/career/collegeVolunteer/campus_wifi.png',
    name: '有校园网',
    show: false,
  },
  {
    icon: 'https://img3.job1001.com/ypMp/elan/images/career/collegeVolunteer/air_conditioner.png',
    name: '有空调',
    show: false,
  },
  {
    icon: 'https://img3.job1001.com/ypMp/elan/images/career/collegeVolunteer/hot_water.png',
    name: '有热水供应',
    show: false,
  },
]); // 设施名字
// 获取当前的路由对象
const route = useRoute();

const getCollegeCampusListFn = async () => {
  const params = {
    collegeId: route.query.dataId,
  };
  const res = await getCollegeCampusList(params);
  if (res.code == 200) {
    campusOptions.value = res.data;
    currentCampus.value = campusOptions.value.length
      ? campusOptions.value[0].id
      : '';
  }
};
const dormitoryDesc = ref('');
const subjectInfoList = ref([
  {
    title: '保研星级',
    num: 1,
  },
  {
    title: '博士学科数量',
    num: 0,
  },
  {
    title: '硕士学科数量',
    num: 0,
  },
  {
    title: '国家一级重点学科',
    num: 0,
  },
]);
const rankList = ref([
  {
    title: '软科排名',
    rank: '0',
  },
  {
    title: '校友会排名',
    rank: '0',
  },
  {
    title: 'QS世界排名',
    rank: '0',
  },
  {
    title: 'US News排名',
    rank: '0',
  },
  {
    title: '泰晤士排名',
    rank: '0',
  },
]);
watch(
  () => currentCampus.value,
  (newVal, oldVal) => {
    // 从 campusOptions.value 中找到 id 等于 newVal 的那一项
    const selectedCampus = campusOptions.value.find(
      (item) => item.id == newVal
    );
    if (selectedCampus) {
      dormitoryDesc.value = selectedCampus.dormitoryDetail;
      if (selectedCampus.bathroom) {
        facilityList.value[0].show = true;
      }
      if (selectedCampus.campusWifi) {
        facilityList.value[1].show = true;
      }
      if (selectedCampus.airConditioner) {
        facilityList.value[2].show = true;
      }
      if (selectedCampus.hotWater) {
        facilityList.value[3].show = true;
      }
    } else {
      console.log('未找到匹配的校区信息');
    }
  },
  { deep: true }
);
const roundStringToArray = (str) => {
  // 将字符串转为浮点数后四舍五入，再放入数组中
  return [Math.round(parseFloat(str))];
};
watch(
  () => props.schoolInfoParams,
  (newParams, oldParams) => {
    if (newParams) {
      rankList.value[0].rank = newParams.ruankeRank;
      rankList.value[1].rank = newParams.xyhRank;
      rankList.value[2].rank = newParams.qsWorld;
      rankList.value[3].rank = newParams.usRank;
      rankList.value[4].rank = newParams.twsChina;

      subjectInfoList.value[0].num = roundStringToArray(
        newParams.recommendMasterLevel
      )[0];
      subjectInfoList.value[0].leftNum = 5 - subjectInfoList.value[0].num;
      subjectInfoList.value[1].num = newParams.numDoctor;
      subjectInfoList.value[2].num = newParams.numMaster;
      subjectInfoList.value[3].num = newParams.numSubject;
      getCollegeCampusListFn();
    }
  },
  { deep: true }
);
onMounted(() => {
  if (props.schoolInfoParams) {
    rankList.value[0].rank = props.schoolInfoParams.ruankeRank;
    rankList.value[1].rank = props.schoolInfoParams.xyhRank;
    rankList.value[2].rank = props.schoolInfoParams.qsWorld;
    rankList.value[3].rank = props.schoolInfoParams.usRank;
    rankList.value[4].rank = props.schoolInfoParams.twsChina;

    subjectInfoList.value[0].num = roundStringToArray(
      props.schoolInfoParams.recommendMasterLevel
    )[0];
    subjectInfoList.value[0].leftNum = 5 - subjectInfoList.value[0].num;
    subjectInfoList.value[1].num = props.schoolInfoParams.numDoctor;
    subjectInfoList.value[2].num = props.schoolInfoParams.numMaster;
    subjectInfoList.value[3].num = props.schoolInfoParams.numSubject;
    getCollegeCampusListFn();
  }
});
</script>

<style lang="scss" scoped>
.school-info {
  width: 100%;
  height: 100%;
  overflow: scroll;
  padding: 40px;
  box-sizing: border-box;
  span {
    margin-right: 22px;
  }
  :deep(.el-select__caret) {
    font-size: 24px !important;
  }
  :deep(.el-select) {
    height: 60px;
  }
  :deep(.el-select__wrapper) {
    height: 100%;
    font-size: 24px;
    background: #f8f8f8;
  }
  .subject-info {
    display: flex;
    gap: 16px;
    margin-bottom: 52px;
    .subject-item {
      width: 350px;
      height: 116px;
      padding: 22px;
      box-sizing: border-box;
      background: #f9f9f9;
      border-radius: 8px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .title {
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 22px;
        color: #555555;
      }
      .num {
        font-family: PingFang-SC, PingFang-SC;
        font-weight: bold;
        font-size: 28px;
        color: #555555;
        span {
          font-size: 21px;
          font-weight: 0;
          margin-left: 4px;
        }
      }
      .stars {
        display: flex;
        gap: 24px;
        .star {
          width: 24px;
          height: 22px;
        }
      }
    }
  }
  .rank-info {
    display: flex;
    gap: 16px;
    margin-bottom: 52px;
    .rank-item {
      width: 176px;
      height: 86px;
      background: #fff9f2;
      border-radius: 13px;
      border: 2px solid #fff0e6;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      .rank {
        font-family: PingFang-SC, PingFang-SC;
        font-weight: bold;
        font-size: 29px;
        color: #222222;
      }
      .title {
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 21px;
        color: #555555;
      }
    }
  }
  .campus-info {
    .bar {
      display: flex;
      align-items: center;
      .title {
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 24px;
        color: #555555;
      }
      .select {
        width: 240px;
      }
    }
  }
  .facility {
    margin-top: 48px;
    display: flex;
    .container {
      display: flex;
      .facility-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 120px;
        height: 116px;
        gap: 16px;
        .icon-container {
          background: #f5f5f5;
          padding: 16px 16px 14px 16px;
          border-radius: 11px;
          .icon {
            width: 38px;
            height: 38px;
          }
        }
        .name {
          font-family: PingFangSC, PingFang SC;
          font-weight: 400;
          font-size: 24px;
          color: #333333;
        }
      }
    }
  }
  .dormitory {
    margin-top: 46px;
    .title {
      font-family: PingFang-SC, PingFang-SC;
      font-weight: bold;
      font-size: 24px;
      color: #333333;

      display: flex;
      align-items: center;
      .logo {
        margin-right: 22px;
        width: 31px;
        height: 31px;
      }
    }
    .desc {
      margin-top: 24px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 24px;
      color: #333333;
    }
  }
}

// 定义变量修改收起展开按钮的颜色，这里以红色为例
:deep(.van-text-ellipsis__action) {
  color: #0b6eff;
}
</style>
